<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信登录回调页面</title>

    <!--    在导入common.js之前要先导入vue和axios否则会报错-->
    <script src="plugins/vue/dist/vue.js"></script>
    <script src="plugins/axios/dist/axios.js"></script>
    <script src="script/common.js"></script>
</head>
<body>
<!--这个回调页面,它只需要做一个事情,就是拿到微信返给我们的code,然后去调后端接口就可以了-->

<div id="app"></div>

<script type="text/javascript">
    new Vue({
        el:"#app",
        data:{

        },
        methods:{
            wxLogin(obj){
                console.log(obj)
                this.$http.post("/login/weChat",obj)
                    .then(result => {
                        result = result.data;
                        console.log(result)
                        let resultObj = result.object;
                        if(!resultObj.message){ // message为false 说明它没有值,说明是登录成功
                            // 提示登录成功
                            alert("登录成功!");
                            //将token与用户信息存入localStorage
                            localStorage.setItem("token",result.object.token);
                            localStorage.setItem("loginInfo", JSON.stringify(result.object.loginInfo));
                        }else if (resultObj.message&&resultObj.message=="noBind") {
                            location.href="/binder.html?accessToken="+resultObj.accessToken+"&openid="+resultObj.openid;
                        }
                    })
            }
        },
        mounted(){
            // 拿到微信返给我们的code,调后端接口
            let obj = urlParseParam();  // code state
            // 调后端接口
            this.wxLogin(obj);
        }
    })
</script>
</body>
</html>